<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/7/26
 * Time: 10:25
 */

namespace app\admin\controller;

use app\admin\controller\User;
use app\admin\model\Marketing as M;
use think\Db;
use \think\Cookie;

class Marketing extends User
{

    public function index(){
        $post=request()->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post=Cookie::get('postlist');
        }else{
            //把条件写入缓存
            Cookie::set('postlist',$post);
        }
        $where='';
        $m=new M();

        if (isset($post['mk_id']) and $post['mk_id'] > 0) {
            $where['mk_id'] = $post['mk_id'];
        }
        if (isset($post['mk_name']) and $post['mk_name'] !='') {
            $where['mk_name']=['like','%'.$post['mk_name'].'%'];
        }

        if (isset($post['mk_status']) and $post['mk_status'] > 0) {
            $where['mk_status'] = $post['mk_status'];
        }


        $data=$m->order('mk_id desc')->where($where)->where('mk_status','NEQ',2)->paginate(20);


        $this->assign('Mk',$data);
        $this->assign('wherelist',$post);
        return $this->fetch();
    }

    public function addmk(){
        $m=new M();
        $id=  input('id');
        if (request()->isPost()){
            $data= request()->request();
            $mk_specs_id='';
//            dump($data);die();
            $specs=$data['data'];

            unset($data['data']);
            Db::startTrans(); //启动事务
            try {


                foreach ($specs as $h){
                    $specs_id= db('specs')->insertGetId([
                        'specs_name'=>$h['specs_name'],
                        'goods_id'=>$data['goods_id'],
                        'specs_types'=>2,
                        'creattime'=>time(),
                        'updatetime'=>time(),
                        'pid'=>$h['specs_id'],
                    ]);


                    $mk_specs_id.=$specs_id.',';
                    foreach ($h['persondata'] as $v){
                        $v['specs_id'] =$specs_id;
                        $v['creattime']=time();
                        $v['updatetime']=time();
                        $v['pid']=$v['person_id'];
                        unset($v['person_id']);
//

                        $person=$m->person()->insert($v);

//                        dump($v);
                    }

//
//
                }


                $e= strtotime($data['mk_endtime'])+86400;
                $marketing= $m->insert(
                    [
                        'mk_starttime'=>strtotime($data['mk_starttime']),
                        'mk_endtime'=>$e,
                        'mk_name'=>$data['mk_name'],
                        'goods_id'=>$data['goods_id'],
                        'specs_id'=>$mk_specs_id
                    ]
                );
//                $specs1=  $m->where('goods_id',$data['goods_id'])->select();






                Db::commit(); //提交事务
                return $this->success('添加成功');
            } catch (\PDOException $e) {
                Db::rollback(); //回滚事务
                return $e->getMessage();
                return $this->error('添加失败');
            }












//            specs 商品规格表
//             人群价格数量表
//            marketing 营销活动表





        }else{


            $goods=$m->goods()->field('goods_id,goods_name')->select();
            $this->assign('trip',$goods);
            return $this->fetch();
        }






    }
    public function xiala(){
        if (request()->isPost()){
            $goods_id= input('goods_id');

            $m=new M();
            $specs= $m->specs()->where('goods_id',$goods_id)->where('specs_status',1)->where('specs_types',1)->select();
            if($specs==true){

                foreach ($specs as $k=>$v){
                    $person=$m->person()->where('specs_id',$v['specs_id'])->where('person_status',1)->field('person_id,person_name,person_num,person_price')->select();

                    $specs[$k]['person']=$person;

                }
            }

            return json_encode($specs);
        }




    }
    public function edit(){
        $id= input('id');
        $m=new M();

        if (request()->isPost()){
            $data=   request()->post();





            $specs=!empty($data['data'])?$data['data']:'';


            $person=[];
            $persons=[];
            $specs_id='';
            unset($data['data']);
            $mk_specs_id='';
            Db::startTrans(); //启动事务
            try {


                if ($specs!=''){


                    foreach ($specs as $v){

                        $v['goods_id']=$data['goods_id'];
                        $v['specs_types']='2';
                        $person=!empty($v['persondata'])?$v['persondata']:'';

                        unset($v['persondata']);
                        $specs_id=!empty($v['specs_id'])?$v['specs_id']:0;
                        unset($v['specs_id']);
                        if ($specs_id>0){
                            $v['updatetime']=time();
                            $specs= $m->specs()->where('specs_id',$specs_id)->update($v);
                            $specs=$specs_id;
                        }else{
                            $v['creattime']=time();
                            $specs= $m->specs()->insertGetId($v);
                        }
                        if ($person!=''){
                            $persons[]=$person;
                        }

                        $mk_specs_id.=$specs.',';
                    }

                }
                $marketing= $m->where('mk_id',$data['mk_id'])->update(
                    [
                        'mk_starttime'=>strtotime($data['mk_starttime']),
                        'mk_endtime'=>strtotime($data['mk_endtime'])+86399,
                        'mk_name'=>$data['mk_name'],
                        'goods_id'=>$data['goods_id'],
                        'specs_id'=>$mk_specs_id
                    ]
                );


                if ($persons!=''){


                    foreach ($persons as $k){

                        foreach ($k as $v){




                            $person_id=!empty($v['person_id'])?$v['person_id']:0;
                            unset($v['person_id']);

                            if ($person_id>0){
                                $v['updatetime']=time();;
                                $person= $m->person()->where('person_id',$person_id)->update($v);
                            }else{
                                $v['creattime']=time();
                                $person= $m->person()->insert($v);
                            }

                        }
                    }
                }
                Db::commit(); //提交事务
                return $this->success('添加成功');
            } catch (\PDOException $e) {
                Db::rollback(); //回滚事务
                return $this->error('添加失败');
            }
        }


        $data= $m->where('mk_id',$id)->find();
        $specs=$m->specs()->where('goods_id',$data['goods_id'])->where('specs_status','1')->where('specs_types',2)->select();
        $data['specs']=$specs;
        foreach ($specs as $v){
            $person=$m->person()->where('specs_id',$v['specs_id'])->select();
            $v['person']=$person;
        }

        $goods=$m->goods()->field('goods_id,goods_name')->select();
        $this->assign('trip',$goods);
        $this->assign('dat',$data);
        return $this->fetch();


    }

    public function del(){
        $m=new M();
        $person_id= input('person_id');//人群
        $specs_id= input('specs_id');//规格

        $data=request()->request();
//    dump($data);
//    die;
        if (!empty($data['mk_id'])){


            Db::startTrans(); //启动事务
            try {
                $del=$m->where('mk_id',$data['mk_id'])->update(['mk_status'=>2]);
                $dal3=$m->specs()->where('goods_id',$data['goods_id'])->where('specs_types',2)->field('specs_id')->select();
                $dal4=$m->specs()->where('goods_id',$data['goods_id'])->where('specs_types',2)->update(['specs_status'=>'2']);
                foreach ($dal3 as $v){
                    $del2=$m->person()->where('specs_id',$v['specs_id'])->update(['person_status'=>2]);
                }
                Db::commit(); //提交事务
                return $this->success('删除成功');
            } catch (\PDOException $e) {
                Db::rollback(); //回滚事务
                return $this->error('删除失败');
            }

        }

        if (!empty($person_id)){//删除人群
//            person_status
            $del=$m->person()->where('person_id',$person_id)->update(['person_status'=>2]);
            if ($del==true){
                return $this->success('删除成功');
            }else{
                return $this->error('删除失败');
            }
        }
        if (!empty($specs_id)){//删除规格
//            person_status
            Db::startTrans(); //启动事务
            try {
                $del=$m->specs()->where('specs_id',$specs_id)->where('specs_types',2)->update(['specs_status'=>2]);
                $del2=$m->person()->where('specs_id',$specs_id)->update(['person_status'=>2]);

                Db::commit(); //提交事务
                return $this->success('删除成功');
            } catch (\PDOException $e) {
                Db::rollback(); //回滚事务
                return $this->error('删除失败');
            }
        }






    }


}